package guda.shop.core.manager.impl;

import guda.shop.common.file.FileWrap;
import guda.shop.core.manager.TemplateMng;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

@Service
@Transactional
public class TemplateMngImpl
        implements TemplateMng {
    private static final Logger logger = LoggerFactory.getLogger(TemplateMngImpl.class);
    private FileFilter resFilter = new FileFilter() {
        public boolean accept(File paramAnonymousFile) {
            if (paramAnonymousFile.isDirectory())
                return true;
            String str = FilenameUtils.getExtension(paramAnonymousFile.getName());
            return FileWrap.allowEdit(str);
        }
    };

    public FileWrap getTplFileWrap(String paramString1, String paramString2) {
        FileWrap localFileWrap = new FileWrap(new File(paramString1), paramString1);
        localFileWrap.setFilename(paramString2);
        return localFileWrap;
    }

    public FileWrap getResFileWrap(String paramString1, String paramString2) {
        FileWrap localFileWrap = new FileWrap(new File(paramString1), paramString1, this.resFilter);
        localFileWrap.setFilename(paramString2);
        return localFileWrap;
    }

    public int uploadResourceFile(String paramString, MultipartFile[] paramArrayOfMultipartFile) {
        if ((paramArrayOfMultipartFile == null) || (paramArrayOfMultipartFile.length == 0))
            return 0;
        File localFile = new File(paramString);
        int i = 0;
        try {
            for (MultipartFile localMultipartFile : paramArrayOfMultipartFile) {
                String str = localMultipartFile.getOriginalFilename();
                if ((!localMultipartFile.isEmpty()) && (allowUpload(FilenameUtils.getExtension(str)))) {
                    localMultipartFile.transferTo(new File(localFile, str));
                    i++;
                }
            }
        } catch (IOException localIOException) {
            logger.error("upload resource failed", localIOException);
        }
        return i;
    }

    public boolean allowUpload(String paramString) {
        return true;
    }
}

